package com.bodu.depress.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.depress.entity.request.DepressSolutionRequest;
import com.bodu.depress.entity.vo.DepressSolutionVo;
import com.bodu.depress.entity.po.DepressSolutionPo;
import com.bodu.core.entity.ParamReq;
import com.bodu.depress.mapper.DepressSolutionMapper;
import com.bodu.depress.service.DepressSolutionService;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目答案 Service impl
 *
 * @author Administrator
 */
@Service
public class DepressSolutionServiceImpl implements DepressSolutionService {

    @Resource
    private DepressSolutionMapper depressSolutionMapper;

    @Override
    public DepressSolutionVo getModel(Long id) {
        return depressSolutionMapper.getModel(id);
    }

    @Transactional
    @Override
    public Long save(DepressSolutionRequest request) {
        depressSolutionMapper.save(request);
        return request.getId();
    }

    @Transactional
    @Override
    public Long update(DepressSolutionRequest request) {
        depressSolutionMapper.update(request);
        return request.getId();
    }

    @Override
    public List<DepressSolutionVo> listPage(ParamReq<DepressSolutionRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        return depressSolutionMapper.listPage(params);
    }

    @Override
    public Integer countPage(ParamReq<DepressSolutionRequest> params) {
        return depressSolutionMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return depressSolutionMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public DepressSolutionVo getSolutionModelByQuestionId(Long questionId) {
        DepressSolutionPo depressSolutionPo = depressSolutionMapper.getSolutionModelByQuestionId(questionId);
        if (ObjectUtil.isEmpty(depressSolutionPo)) {
            return null;
        }

        DepressSolutionVo depressSolutionVo = new DepressSolutionVo();
        BeanUtils.copyProperties(depressSolutionPo, depressSolutionVo);
        return depressSolutionVo;
    }
}
